﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FrmSupplyWaterCal : System.Windows.Forms.Form
	{
		public FrmSupplyWaterCal(Document document)
		{
			this.InitializeComponent();
			this.doc = document;
			this.userSetting = new HVACUserSetting();
			//base.Icon = Resource.YArchitechIcon;
		}

		private void FrmSupplyWaterCal_Load(object sender, EventArgs e)
		{
			this.InitialInterfaceInfo();
			this.BindingPipeType(this.pipeTypeName);
			ElementId elemId = this.cmbPipeType.SelectedValue as ElementId;
			this.BindingDiameter(elemId, this.diameter);
		}

		private void BindingPipeType(string selectTex)
		{
			IList<PipeType> pipeTypes = new HYSystemElementFilter(this.doc).GetPipeTypes();
			this.cmbPipeType.DataSource = pipeTypes;
			this.cmbPipeType.DisplayMember = "Name";
			this.cmbPipeType.ValueMember = "Id";
			int selectedIndex = 0;
			for (int i = 0; i < this.cmbPipeType.Items.Count; i++)
			{
				if ((this.cmbPipeType.Items[i] as PipeType).Name == selectTex)
				{
					selectedIndex = i;
					break;
				}
			}
			this.cmbPipeType.SelectedIndex = selectedIndex;
		}

		private void BindingDiameter(ElementId elemId, double nDiameter)
		{
			if (elemId == null)
			{
				return;
			}
			PipeType pipeType = this.doc.GetElementById(elemId) as PipeType;
			IList<PipeSize> ptmepsize = this.GetPTMEPSize(pipeType);
			this.cmbDiameter.DataSource = ptmepsize;
			this.cmbDiameter.DisplayMember = "DN";
			this.cmbDiameter.ValueMember = "InDiameter";
			this.cmbDiameter.Refresh();
			int selectedIndex = 0;
			for (int i = 0; i < this.cmbDiameter.Items.Count; i++)
			{
				if ((this.cmbDiameter.Items[i] as PipeSize).DN == nDiameter)
				{
					selectedIndex = i;
					break;
				}
			}
			this.cmbDiameter.SelectedIndex = selectedIndex;
		}

		private IList<PipeSize> GetPTMEPSize(PipeType pipeType)
		{
			IList<PipeSize> list = new List<PipeSize>();
			foreach (MEPSize mepsize in UnifiedModified.GetAllMepSize(pipeType))
			{
				list.Add(new PipeSize
				{
					DN = Convert.ToDouble(StringTools.CutOffEfficacious((mepsize.NominalDiameter * 304.8).ToString(), 0)),
					InDiameter = mepsize.InnerDiameter * 304.8
				});
			}
			return list;
		}

		private void cmbPipeType_SelectedIndexChanged(object sender, EventArgs e)
		{
			ElementId elemId = this.cmbPipeType.SelectedValue as ElementId;
			this.BindingDiameter(elemId, this.diameter);
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			double num = Convert.ToDouble(this.txtFSpeed.Text);
			if (num <= 0.0 || num > 100.0)
			{
				YJKMessageBox.Show("流速应在0～100之间");
				this.txtFSpeed.Select();
				this.txtFSpeed.Focus();
				return;
			}
			num *= 3600.0;
			double num2 = Convert.ToDouble(this.cmbDiameter.SelectedValue) / 1000.0;
			double num3 = Math.PI * num2 * num2 / 4.0;
			this.qg = num3 * num;
			this.qg = Convert.ToDouble(StringTools.CutOffEfficacious(this.qg.ToString(), 2));
			this.SaveInterfaceInfo();
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void InitialInterfaceInfo()
		{
			IList<string> supplyWaterCal = this.userSetting.GetSupplyWaterCal();
			this.pipeTypeName = supplyWaterCal[0];
			this.diameter = Convert.ToDouble(supplyWaterCal[1]);
			this.txtFSpeed.Text = supplyWaterCal[2];
		}

		private void SaveInterfaceInfo()
		{
			IList<string> list = new List<string>();
			PipeType pipeType = this.cmbPipeType.SelectedItem as PipeType;
			list.Add(pipeType.Name);
			PipeSize pipeSize = this.cmbDiameter.SelectedItem as PipeSize;
			list.Add(pipeSize.DN.ToString());
			list.Add(this.txtFSpeed.Text);
			this.userSetting.WriteSupplyWaterCal(list);
		}

		public double Qg
		{
			get
			{
				return this.qg;
			}
			set
			{
				this.qg = value;
			}
		}

		private void btnCancel_Click_1(object sender, EventArgs e)
		{
			base.Close();
		}

		private void txtFSpeed_Validating(object sender, CancelEventArgs e)
		{
			if (string.IsNullOrEmpty(this.txtFSpeed.Text))
			{
				YJKMessageBox.Warning("流速不能为空！");
				this.txtFSpeed.Text = "10";
				e.Cancel = true;
			}
		}

		private Document doc;

		private HVACUserSetting userSetting;

		private string pipeTypeName = "";

		private double diameter;

		private double qg;
	}
}
